
// 翻页类
class Page {
    // 把控制页面翻页状态的对象存入此对象内
    // 
    //   pageObject = {
    //     a: {},
    //     b: {},
    //     .....
    //     }
    // 
    pageObject;
    constructor(pageObject) {
        this.pageObject = pageObject
    }
}

//访问过的翻页类
class VisitedPage extends Page {
    routerPath;
    routerName;
    constructor(pageObject, routerPath, routerName) {
        super(pageObject);
        this.routerPath = routerPath ? routerPath : null;
        this.routerName = routerName ? routerName : null;
    }
}

export const modulePage = {
    state: {
        //存储各个页面的翻页信息
        visitedPages: [],
        //当前路由地址
        currentPath: null,
    },
    getters: {
        //获取此页面翻页的历史信息
        visitedPage: state => routerPath => {
            state.currentPath = routerPath;
            return state.visitedPages.find(visitedPage => {
                if (visitedPage.routerPath === routerPath) {
                    if (!visitedPage.pageObject) {
                        return null;
                    }
                    return visitedPage;
                }
            });
        }
    },
    mutations: {
        //添加此页面翻页信息，如已经存在就更新
        addVisitedPages(state, pageObject) {
            const lastPage = state.visitedPages.find(element => {
                if (element.routerPath == state.currentPath) {
                    Object.assign(element, {
                        routerPath: state.currentPath,
                        pageObject: pageObject,
                    })
                    return element;
                }
            })

            //判断此页面翻页信息不存在，把它push到store 
            if (!lastPage) {
                state.visitedPages.push(new VisitedPage(pageObject, state.currentPath));
            }
        }
    },
    actions: {
    }
}